package com.easy.web.controller.common;

import com.easy.common.core.R;
import com.easy.common.core.domain.model.RegisteredBody;
import com.easy.common.utils.AccountTypeCheckUtils;
import com.easy.common.utils.mail.MailUtils;
import com.easy.framework.UserVerify;
import com.easy.user.service.IUserRegisteredService;
import com.easy.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.easy.common.constant.ResultConstant.ACCOUNT_NONENTITY;
import static com.easy.common.constant.ResultConstant.REGISTRATION_ERROR;

/**
 * 用户 自助服务 控制器
 *
 * @author RuoYu
 * @date 2022/11/23
 */
@RestController
@RequestMapping("/self_services")
@Api(tags = "用户自助服务相关接口")
public class UserSelfServicesController {

    private static final Logger log = LoggerFactory.getLogger(UserSelfServicesController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private MailUtils mailUtils;

    @Autowired
    private IUserRegisteredService iUserRegisteredService;

    @Autowired
    private UserVerify userVerify;

    /**
     * 找回密码
     *
     * @param account 手机号（邮箱号）
     * @return {@link R}
     */
    @GetMapping("/reset_account")
    @ApiOperation(value = "重置密码")
    @ApiImplicitParam(name = "account", value = "手机号（邮箱号）", dataTypeClass = String.class, required = true)
    public R resetPassword(String account) {

        int accountType = AccountTypeCheckUtils.accountType(account);

        // 1. 判断账户是否存在
        if (accountType == 0) {
            boolean flag = userService.checkUserPhone(account);
            // 2. 存在
            if (flag) {

            }
        } else if (accountType == 1) {
            boolean flag = userService.checkUserEmail(account);
            // 2. 存在
            if (flag) {
                mailUtils.sendMail(account);
                return R.success();
            }
        }
        return R.error(ACCOUNT_NONENTITY);
    }

    /**
     * 重置用户密码
     *
     * @param account  账户
     * @param password 密码
     * @return {@link R}
     */
    @PutMapping("/password/edit")
    @ApiOperation(value = "重置用户密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账户", dataTypeClass = String.class, required = true),
            @ApiImplicitParam(name = "password", value = "密码", dataTypeClass = String.class, required = true)
    })
    public R editPassword(String account, String password) {
        int accountType = AccountTypeCheckUtils.accountType(account);
        if (accountType == 0) {
            int row = userService.updateUserPasswordByPhone(account, password);
            if (row > 0) {
                return R.success();
            }
        } else if (accountType == 1) {
            int row = userService.updateUserPasswordByEmail(account, password);
            if (row > 0) {
                return R.success();
            }
        }
        return R.error();
    }


    /**
     * 注册用户
     *
     * @param registeredBody 注册信息
     * @return {@link R}
     */
    @PostMapping("/registered")
    @ApiOperation(value = "注册用户")
    @ApiImplicitParam(name = "registeredBody", value = "注册信息", required = true)
    public R registeredUser(@RequestBody RegisteredBody registeredBody) {
//        log.info(registeredBody.toString());

        // 1. 校验是否可以注册
        String msg = userVerify.registeredVerify(registeredBody);
        log.info("校验是否可以注册:{}",msg);
        if (msg.equals("OK")) {
            // 1.2. 注册
            boolean result = iUserRegisteredService.UserRegistered(registeredBody);
            // 1.3.注册成功
            if (result) {
                return R.success();
            } else {
                // 1.4.注册失败
                log.error("BB001{}", REGISTRATION_ERROR);
                return R.error();
            }
        }
        // 2.校验失败
        return R.error(msg);
    }
}
